package com.yami.shop.bean.serializer.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.yami.shop.bean.app.dto.FaceDesignConfigDto;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * FaceDesignConfigDto JSON序列化器
 * @author AI Assistant
 */
@Slf4j
public class FaceDesignConfigSerializer extends JsonSerializer<FaceDesignConfigDto> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void serialize(FaceDesignConfigDto value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException {

        if (value == null) {
            gen.writeNull();
            return;
        }

        try {
            // 将FaceDesignConfigDto对象序列化为JSON字符串
            String jsonString = objectMapper.writeValueAsString(value);
            gen.writeString(jsonString);
        } catch (JsonProcessingException e) {
            log.error("FaceDesignConfigDto序列化失败: {}", e.getMessage(), e);
            gen.writeNull();
        }
    }
}
